/******************************************
Cola de funciones
Archivo: queue_fun.c, LAB 6 SISEM, 5/5/2011, sebfer, juliano
*****************************************/

#include "queue_fun.h"

static int head;
static int tail;
static pf_t q_elements[QUEUE_SIZE_FUN];

void q_fun_init(){
  // reset position index
  head = 0; 
  tail = 0;
}

void q_fun_add(pf_t in){
//  assert(!q_isfull()); // if queue if full => abort
  q_elements[head] = in;
  head++;
  if (head == QUEUE_SIZE_FUN){
    head = 0;
  }
}

pf_t q_fun_get(){
  pf_t data_to_return;
//  assert(!q_isempty()); // if queue if empty => abort
  data_to_return = q_elements[tail];
  tail++;
  if (tail == QUEUE_SIZE_FUN){
     tail = 0;
  }
  return data_to_return;
}

int q_fun_isempty(){
  if  (tail == head ){
    return 1; // true, queue is empty
  } else {
    return 0; // false, queue is not empty
  }
}

int q_fun_isfull(){
  if ( (head+1 == tail) || ((head+1 == QUEUE_SIZE_FUN) && (tail == 0) ) ){
    return 1; // true, queue is full
  } else {
    return 0; // false, queue is not full
  }
}